package com.gxs.tga.poc.rest;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gxs.tga.poc.domain.Test;
import com.gxs.tga.poc.domain.exceptions.TestNotFound;
import com.gxs.tga.poc.rest.common.TgaRestResponse;
import com.gxs.tga.poc.service.TestService;

@Component("testRestBean")
@Path("/tests")
public class TestRestBean
{
	@Autowired
	private TestService testService;

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Response getAllTests()
	{
		List<Test> list = this.testService.findAll();
		return Response.status(Status.OK).entity(list).build();
	}

	@GET
	@Path("/{testId}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getSingleTestById(@PathParam("testId") Long testId)
	{
		Test test = this.testService.findById(testId);
		if (test == null)
		{
			int statusCode = 404;
			TgaRestResponse response = new TgaRestResponse(statusCode, "No record of type Test exists with the id: " + testId, statusCode);
			return Response.status(statusCode).entity(response).build();
		} else
		{
			return Response.status(200).entity(test).build();
		}

	}

	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response createOrUpdateTest(Test test, @QueryParam("operation") String operation) 
	{
 
		String result = "Test being created: " + test.getName();
		testService.create(test);
		System.out.println("==>  Operation: " + operation);
		int statusCode  = Response.Status.CREATED.getStatusCode();
		
		return Response.status(statusCode).entity(test).build();
	}
 
}
